package com.miyake.dsp.osa.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import com.miyake.dsp.osa.ActResElement;

public abstract class ActualResolutionUI extends JFrame {
	abstract public void onSet(Map<Double, Map<Integer, Double>> aMap);
	private DefaultTableModel model;

	public static void main(String[] arg) {
		Map<Double, Map<Integer, Double>> actResMap = new HashMap<Double, Map<Integer, Double>>();
		Map<Integer, Double> e1 = new HashMap<Integer, Double>();
		e1.put(600, 0.03);
		e1.put(1000, 0.03);
		e1.put(1400, 0.03);
		e1.put(1650, 0.03);
		actResMap.put(0.03, e1);

		Map<Integer, Double> e2 = new HashMap<Integer, Double>();
		e2.put(600, 0.05);
		e2.put(1000, 0.05);
		e2.put(1400, 0.05);
		e2.put(1650, 0.05);
		actResMap.put(0.05, e2);

		Map<Integer, Double> e3 = new HashMap<Integer, Double>();
		e3.put(600, 0.07);
		e3.put(1000, 0.07);
		e3.put(1400, 0.07);
		e3.put(1650, 0.07);
		actResMap.put(0.07, e3);
	
		Map<Integer, Double> e4 = new HashMap<Integer, Double>();
		e4.put(600, 0.1);
		e4.put(1000, 0.1);
		e4.put(1400, 0.1);
		e4.put(1650, 0.1);
		actResMap.put(0.1, e4);

		Map<Integer, Double> e5 = new HashMap<Integer, Double>();
		e5.put(600, 0.2);
		e5.put(1000, 0.2);
		e5.put(1400, 0.2);
		e5.put(1650, 0.2);
		actResMap.put(0.2, e5);
		
		Map<Integer, Double> e6 = new HashMap<Integer, Double>();
		e6.put(600, 0.5);
		e6.put(1000, 0.5);
		e6.put(1400, 0.5);
		e6.put(1650, 0.5);
		actResMap.put(0.5, e6);
		
		Map<Integer, Double> e7 = new HashMap<Integer, Double>();
		e7.put(600, 1.0);
		e7.put(1000, 1.0);
		e7.put(1400, 1.0);
		e7.put(1650, 1.0);
		actResMap.put(1.0, e7);
		ActualResolutionUI ui = new ActualResolutionUI(actResMap){

			@Override
			public void onSet(Map<Double, Map<Integer, Double>> aMap) {
				// TODO Auto-generated method stub
				System.out.println(aMap);
			}		
		};
		ui.setVisible(true);
	}
	public ActualResolutionUI(Map<Double, Map<Integer, Double>> map) {
		initialize(map);
	}

	private void initialize(Map<Double, Map<Integer, Double>> map) {
		this.setSize(new Dimension(500, 300));
		this.setTitle("Actual Resolution");
		Container c = this.getContentPane();
		c.setLayout(new FlowLayout());
		c.add(createTable(map));
		c.add(createOk());
		//pack();
	}

	private Component createOk() {
		JPanel p = new JPanel();
		JButton ok = new JButton("Set");
		p.add(ok);
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				set();
			}
		});
		return p;
	}

	private Double[] res = {0.03, 0.05, 0.07, 0.1, 0.2, 0.5, 1.0};
	private Integer[] wav = {600, 1000, 1400, 1650};
	
	protected void set() {
		Map<Double, Map<Integer, Double>> aMap = new HashMap<Double, Map<Integer, Double>>();
		
		for (int row = 0; row < model.getRowCount(); row++) {
			Map<Integer, Double> e = new HashMap<Integer, Double>();
			for (int col = 0; col < model.getColumnCount(); col++) {
				if (col == 0) {
					
				}
				else {
					e.put(wav[col-1], Double.valueOf((String)model.getValueAt(row, col)));
				}
			}
			aMap.put(res[row], e);
		}
		onSet(aMap);
	}
	private Component createTable(Map<Double, Map<Integer, Double>> map) {
		
		Vector<Integer> title = new Vector<Integer>();
		title.add(null);
		for (int i = 0; i < wav.length; i++) {
			title.add(wav[i]);
		}
		
		model = new DefaultTableModel(title, 0) {

			@Override
			public boolean isCellEditable(int arg0, int arg1) {
				if (arg1 >0 && arg1 <=  wav.length) {
					return true;
				}
				return false;
			}
			
		};

		JTable table = new JTable(model);
		model.addTableModelListener(new TableModelListener(){
			@Override
			public void tableChanged(TableModelEvent arg0) {

			}
			
		});
		//table.setPreferredSize(new Dimension(200, 200));
		for (int row = 0; row < res.length; row++) {
			Map<Integer, Double> m = map.get(res[row]);
			model.setRowCount(model.getRowCount()+1);
			for (int col = 0; col < wav.length + 1; col++) {
				
				if (col == 0) {
					model.setValueAt(res[row], row, col);
				}
				else {
					Double d = m.get(wav[col-1]);
					model.setValueAt(d.toString(), row, col);
				}
			}
		}
		
		JScrollPane s = new JScrollPane(table);
		s.setPreferredSize(new Dimension(400,200));
		return s;
	}
}
